﻿using log4net;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Mail;
using System.Text;
using System.Threading.Tasks;

namespace EasyTask.EasyHelper
{


    public static class MailHelper
    {
        private static ILog logger = LogManager.GetLogger(nameof(MailHelper));


        /// <summary>
        /// 邮件服务器地址
        /// </summary>
        private static string MailServer = Config.GetValue("MailHost");
        /// <summary>
        /// 用户名
        /// </summary>
        private static string MailUserName = Config.GetValue("MailUserName");
        /// <summary>
        /// 密码
        /// </summary>
        private static string MailPassword = Config.GetValue("MailPassword");
        /// <summary>
        /// 名称
        /// </summary>
        private static string MailName = Config.GetValue("MailName");
        /// <summary>
        /// 发送邮件
        /// </summary>
        /// <param name="to">收件人邮箱地址,多个按照逗号分隔</param>
        /// <param name="subject">主题</param>
        /// <param name="body">内容</param>
        /// <param name="encoding">编码</param>
        /// <param name="isBodyHtml">是否Html</param>
        /// <param name="enableSsl">是否SSL加密连接</param>
        public static async Task SendAsync(string to, string subject, string body, string encoding = "UTF-8", bool isBodyHtml = true, bool enableSsl = false)
        {
            logger.Info("开始发送邮件");
            var tos = to.Split(',');
            foreach (var x in tos)
            {
                if (!string.IsNullOrEmpty(x))
                {
                    logger.Info("开始发送邮件,x="+x);

                    await SendSingleAsync(x, subject, body, encoding, isBodyHtml, enableSsl);
                }
            }
        }

        /// <summary>
        /// 同步发送邮件
        /// </summary>
        /// <param name="to">收件人邮箱地址,多个按照逗号分隔</param>
        /// <param name="subject">主题</param>
        /// <param name="body">内容</param>
        /// <param name="encoding">编码</param>
        /// <param name="isBodyHtml">是否Html</param>
        /// <param name="enableSsl">是否SSL加密连接</param>
        private static async Task SendSingleAsync(string to, string subject, string body, string encoding = "UTF-8", bool isBodyHtml = true, bool enableSsl = false)
        {
            logger.Info("开始发送邮件,SendSingleAsync,x=" + to);

            MailMessage message = new MailMessage();
            // 接收人邮箱地址
            message.To.Add(new MailAddress(to));
            message.From = new MailAddress(MailUserName, MailName);
            message.BodyEncoding = Encoding.GetEncoding(encoding);
            message.Body = body;
            //GB2312
            message.SubjectEncoding = Encoding.GetEncoding(encoding);
            message.Subject = subject;
            message.IsBodyHtml = isBodyHtml;

            SmtpClient smtpclient = new SmtpClient(MailServer, 25)
            {
                Credentials = new System.Net.NetworkCredential(MailUserName, MailPassword),
                //SSL连接
                EnableSsl = enableSsl
            };
            await smtpclient.SendMailAsync(message);
            logger.Info("开始发送邮件,await SendSingleAsync,x=" + to);
        }
    }
}
